var session = sessionStorage.getItem("session");
if (!session) {
	window.parent.location = "../../login.html";
}
$(document).ready(function() {
	$("#paraSendEmail").jqGrid({
		url: IP + "GetProbeParam?callback=?&paramtype=SendEmail&session=" + session,
		mtype: "Get",
		datatype: "jsonp",
		colModel: [{
			label: '序号',
			sorttype: 'integer',
			name: 'id',
			key: true,
			width: 50,
			hidden: true
		}, {
			label: '测试类型',
			name: 'testtype',
			hidden: true
		}, {
			label: '单次测试超时(s)',
			name: 'timeout'
		}, {
			label: '目标地址',
			name: 'destination'
		}, {
			label: '服务端口',
			name: 'serverport'
		}, {
			label: '用户',
			name: 'user'
		}, {
			label: '端口',
			name: 'port'
		}, {
			label: '上传文件',
			name: 'uploadlocalfile'
		}, {
			label: '发送人',
			name: 'mailfrom'
		}, {
			label: '接收人',
			name: 'mailrecipients'
		}, {
			label: '主题',
			name: 'subject'
		}, {
			label: '内容',
			name: 'content'
		}, {
			label: '附件大小',
			name: 'attachmentsize'
		}, {
			label: '附件类型',
			name: 'attachmenttype'
		}, {
			label: '开始时间',
			name: 'startTime'
		}, {
			label: '结束时间',
			name: 'endTime'
		}],
		loadonce: true,
		height: 310,
		rowNum: 17,
		rownumbers: true,
		autowidth: true,
		pagerpos: 'center',
		page: 1,
		viewrecords: true,
		pgbuttons: true,
		pginput: true,
		loadtext: '正在加载数据',
		pager: "#paraSendEmailPager"
	});

	$(window).bind('resize', function() {
		$("#paraSendEmail").setGridWidth($(window).width());
		$("#paraSendEmail").setGridHeight($(window).height()-260);
	});
	ExportFile = function() {
		$('#paraSendEmail').jqGrid('setGridParam', {
			rowNum: 1000,
		}).trigger('reloadGrid');
		var title = "邮件发送参数";
		getXlsFromTbl('paraSendEmail', 'gridtable', title, true);
		setTimeout(BackGrid(), 500);

		function BackGrid() {
			$('#paraSendEmail').jqGrid('setGridParam', {
				rowNum: 17,
			}).trigger('reloadGrid');
		}
	}
	document.onkeydown = function() {
		if (event.ctrlKey && event.keyCode == 13) {
			SaveRow();
		}
	}
	var template1 = {
		"groupOp": "AND",
		"rules": [{
			"field": "destination",
			"op": "eq",
			"data": "W"
		}, {
			"field": "count",
			"op": "le",
			"data": "15"
		}]
	};
	// activate the build in search with multiple option
	$('#paraSendEmail').navGrid("#paraSendEmailPager", {
			search: true, // show search button on the toolbar
			add: false,
			edit: false,
			del: false,
			refresh: true
		}, {}, // edit options
		{}, // add options
		{}, // delete options
		{
			multipleSearch: true,
			multipleGroup: true,
			showQuery: true,
			// set the names of the template
			tmplNames: ["模板一"],
			// set the template contents
			tmplFilters: [template1]
		} // search options - define multiple search
	);
	AddRow();
});

function selectRow() {
	jQuery('#paraSendEmail').jqGrid('setSelection', '2');
}

function AddRow() {
	$('#paraSendEmailForm').form('load', {
		id: 0,
		testtype: 'SendEmail',
		timeout: '',
		destination: '',
		serverport: '',
		user: '',
		port: '',
		uploadlocalfile: '',
		mailfrom: '',
		mailrecipients: '',
		subject: '',
		content: '',
		attachmentsize: '',
		attachmenttype: '',
		startTime: '00:00',
		endTime: '23:59',


	});
	$('#paraSendEmail').jqGrid('setSelection', id);
}

function EditRow() {
	var grid = $("#paraSendEmail");
	var rowKey = grid.jqGrid('getGridParam', "selrow");
	if (rowKey) {
		$('#paraSendEmailForm').form('load', {
			id: $("#paraSendEmail").getCell(rowKey, "id"),
			testtype: $("#paraSendEmail").getCell(rowKey, "testtype"),
			timeout: $("#paraSendEmail").getCell(rowKey, "timeout"),
			destination: $("#paraSendEmail").getCell(rowKey, "destination"),
			serverport: $("#paraSendEmail").getCell(rowKey, "serverport"),
			user: $("#paraSendEmail").getCell(rowKey, "user"),
			port: $("#paraSendEmail").getCell(rowKey, "port"),
			uploadlocalfile: $("#paraSendEmail").getCell(rowKey, "uploadlocalfile"),
			mailfrom: $("#paraSendEmail").getCell(rowKey, "mailfrom"),
			mailrecipients: $("#paraSendEmail").getCell(rowKey, "mailrecipients"),
			subject: $("#paraSendEmail").getCell(rowKey, "subject"),
			content: $("#paraSendEmail").getCell(rowKey, "content"),
			attachmentsize: $("#paraSendEmail").getCell(rowKey, "attachmentsize"),
			attachmenttype: $("#paraSendEmail").getCell(rowKey, "attachmenttype"),
			startTime: $("#paraSendEmail").getCell(rowKey, "startTime"),
			endTime: $("#paraSendEmail").getCell(rowKey, "endTime"),

		});
	} else {
		$.messager.alert('提醒', "没有选中行", 'info');
	}
}

function DeleteRow() {
	var grid = $("#paraSendEmail");
	var rowKey = grid.jqGrid('getGridParam', "selrow");
	var oldvalue = $("#paraSendEmail").getCell(rowKey, "destination");
	if (rowKey) {
		$.messager.confirm('删除提醒', '你确认要删除邮件发送测试参数---' + oldvalue + '?', function(r) {
			if (r) {

				$.ajax({
					type: "post", // 与可以 "get"
					url: IP + "DeleteProbeParam?callback=?&session=" + session,
					data: {
						id: rowKey,
						session: session,
					},
					dataType: "jsonp",
					jsonp: "callback",
					jsonpCallback: "resultHandler",
					success: function(json) {
						if (json.result == 200) {
							$("#paraSendEmail").delRowData(rowKey);
							insertopertlog('任务管理', '邮件发送测试参数', '删除', oldvalue, '');
							$.messager.show({
								title: '提醒',
								msg: '删除成功',
								timeout: 1300,
								showType: 'slide',
								style: {
									right: '',
									bottom: ''
								}
							});
						} else {
							$.messager.alert('删除失败', json.message, 'error');
						}
					}
				});
			}
		});
	} else {
		$.messager.alert('提醒', "没有选中行", 'info');
	}
}

function SaveRow() {
	var grid = $("#paraSendEmail");
	var rowKey = grid.jqGrid('getGridParam', "selrow");
	var oldvalue = grid.getCell(rowKey, "destination");
	if ($('#paraSendEmailForm').form('enableValidation').form('validate')) {
		$.ajax({
			type: "post", // 与可以 "get"
			url: IP + "UpdateSendEmailParam?callback=?&session=" + session,
			data: {
				id: $('#id').val(),
				testtype: $("#testtype").val(),
				timeout: $("#timeout").val(),
				destination: $("#destination").val(),
				user: $("#user").val(),
				port: $("#port").val(),
				serverport: $("#serverport").val(),
				uploadlocalfile: $("#uploadlocalfile").val(),
				mailfrom: $("#mailfrom").val(),
				mailrecipients: $("#mailrecipients").val(),
				subject: $("#subject").val(),
				content: $("#content").val(),
				attachmentsize: $("#attachmentsize").val(),
				attachmenttype: $("#attachmenttype").val(),
				startTime: $("#startTime").val(),
				endTime: $("#endTime").val(),
			},
			dataType: "jsonp",
			jsonp: "callback",
			jsonpCallback: "resultHandler",
			success: function(json) {
				if (json.result == 200) {
					if ($('#id').val() == 0) {
						$("#paraSendEmail").addRowData(json.id, {
							id: json.id,
							testtype: $("#testtype").val(),
							timeout: $("#timeout").val(),
							destination: $("#destination").val(),
							serverport: $("#serverport").val(),
							user: $("#user").val(),
							port: $("#port").val(),
							uploadlocalfile: $("#uploadlocalfile").val(),
							mailfrom: $("#mailfrom").val(),
							mailrecipients: $("#mailrecipients").val(),
							subject: $("#subject").val(),
							content: $("#content").val(),
							attachmentsize: $("#attachmentsize").val(),
							attachmenttype: $("#attachmenttype").val(),
							startTime: $("#startTime").val(),
							endTime: $("#endTime").val(),
						}, '');
						insertopertlog('任务管理', '邮件发送测试参数', '新增', '', $("#destination").val());
						$.messager.show({
							title: '提醒',
							msg: '邮件发送测试参数增加成功',
							timeout: 1300,
							showType: 'slide',
							style: {
								right: '',
								bottom: ''
							}
						});
					} else {
						$("#paraSendEmail").setRowData($('#id').val(), {
							id: $('#id').val(),
							testtype: $("#testtype").val(),
							timeout: $("#timeout").val(),
							destination: $("#destination").val(),
							user: $("#user").val(),
							port: $("#port").val(),
							uploadlocalfile: $("#uploadlocalfile").val(),
							serverport: $("#serverport").val(),
							mailfrom: $("#mailfrom").val(),
							mailrecipients: $("#mailrecipients").val(),
							subject: $("#subject").val(),
							content: $("#content").val(),
							attachmentsize: $("#attachmentsize").val(),
							attachmenttype: $("#attachmenttype").val(),
							startTime: $("#startTime").val(),
							endTime: $("#endTime").val(),
						}, '');
						insertopertlog('任务管理', '邮件发送测试参数', '修改', oldvalue, $("#destination").val());
						$.messager.show({
							title: '提醒',
							msg: '邮件发送测试参数修改成功',
							timeout: 1300,
							showType: 'slide',
							style: {
								right: '',
								bottom: ''
							}
						});
					}
				} else {
					$.messager.alert('保存失败', json.message, 'error');
				}
				//保存成功后，重置控件，以免用户重复提交
				AddRow();
			}
		});
	}
}